package com.inmaytide.website.controller.system;

import com.inmaytide.website.domain.entities.system.SysLog;
import com.inmaytide.website.service.system.SysLogService;
import com.inmaytide.website.utils.DateUtils;
import com.inmaytide.website.utils.log.annotation.LogInformation;
import com.inmaytide.website.utils.pagination.Pagination;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author Moss
 * @version 1.0.0
 * @since May 22, 2016
 */
@Controller
@RequestMapping("sys/log")
public class LogController {

    @Resource
    private SysLogService logService;

    @RequestMapping("list")
    @RequiresPermissions("log:view")
    public String findList(HttpServletRequest request, Pagination<SysLog> pagination, ModelMap model) {
        model.put("pagination", logService.findList(request, pagination));
        return "sys/log/list_log";
    }

    @RequestMapping("clear")
    @RequiresPermissions("log:clear")
    public String clear(HttpServletRequest request, ModelMap model) {
        logService.deleteBatch();
        model.put("pagination", new Pagination<SysLog>());
        return "sys/log/list_log";
    }

    @RequestMapping("export")
    @RequiresPermissions("log:export")
    @LogInformation(description = "导出日志")
    public void export(HttpServletRequest request, HttpServletResponse response, String type, Pagination<SysLog> pagination) throws IOException {
        pagination.setConditions(logService.createConditions(request));
        String fileDisplayName = String.format("系统操作日志%s.xls", DateUtils.getDateStr());
        fileDisplayName = URLEncoder.encode(fileDisplayName, "utf-8");
        response.setContentType("application/x-download");
        response.addHeader("Content-Disposition", "attachment;filename=" + fileDisplayName);
        logService.export(type, pagination, response.getOutputStream());
    }

}
